package cn.bby.tools.actions;

import cn.bby.tools.dialog.GenEntityFieldDialog;
import cn.bby.tools.dialog.SingleCheckDialog;
import cn.bby.tools.utils.AnActionEventUtils;
import cn.bby.tools.utils.MessageUtil;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MessageDialogBuilder;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiJavaFile;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @description: GenEntityFieldAction
 * @author: ben
 * @date: 2025/10/15 14:00
 * @version: 1.0
 */
public class GenEntityFieldAction extends AnAction {
    private static final Logger log = LoggerFactory.getLogger(GenEntityFieldAction.class);

    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        String selectedText = AnActionEventUtils.getSelectedText(e);
        Project project = e.getProject();
        if (project == null) {
            return;
        }
        PsiFile currentFile = AnActionEventUtils.getCurrentFile(e);

        GenEntityFieldDialog dialog = new GenEntityFieldDialog(project);
        if (currentFile instanceof PsiJavaFile javaFile) {
            SingleCheckDialog checkDialog = new SingleCheckDialog(project, "message.CurrentClassQuestion",
                    Arrays.asList("message.SourceClass", "message.TargetClass", "message.NotBoth"), 2);
            checkDialog.show();
            int selectedIndex = checkDialog.getSelectedIndex();
            if (selectedIndex == 0) {
                dialog.setSourceClass(javaFile.getClasses()[0].getQualifiedName());
            } else if (selectedIndex == 1) {
                dialog.setTargetClass(javaFile.getClasses()[0].getQualifiedName());
            }
        }
        dialog.setSelectedText(selectedText);
        dialog.show();
    }

}
